package com.game.majiang.vo;

import java.sql.Types;
import java.util.ArrayList;

import javax.sql.rowset.CachedRowSet;

import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;
import com.game.server.db.entity.ProxyTask;

public class ProxyTaskVo {

	
	public static void insertInto(int uid, int taskid, long count, long taskstarttime) {
		String sql = "INSERT INTO proxytask (uid,taskid,count,taskstarttime) VALUES (?,?,?,?)";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, taskid, ParameterType.Input);
		Parameter p3 = new Parameter(Types.BIGINT, count, ParameterType.Input);
		Parameter p4 = new Parameter(Types.BIGINT, taskstarttime, ParameterType.Input);
		SqlHelperwrite.execute(sql, p1,p2,p3,p4);
	}
	//添加任务量
	public static void updateAddCount(int id, long count) {
		String sql = "UPDATE proxytask SET count = count +? WHERE id = ?";
		Parameter p1 = new Parameter(Types.BIGINT, count, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelperwrite.execute(sql, p1,p2);
	}
	//任务重置
	public static void resetTask(int id, long taskstarttime) {
		String sql = "UPDATE proxytask SET receive=0,count = 0,taskstarttime=? WHERE taskid = ?";
		Parameter p1 = new Parameter(Types.BIGINT, taskstarttime, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelperwrite.execute(sql, p1,p2);
	}
	
	//删除任务
	public static void deleteTaskid(int taskid) {
		String sql ="DELETE FROM proxytask WHERE taskid = ?";
		Parameter p1 = new Parameter(Types.INTEGER, taskid, ParameterType.Input);
		SqlHelperwrite.execute(sql,p1);
	}
	
	public static ArrayList<ProxyTask> select() {
		ArrayList<ProxyTask> listTask = new ArrayList<>();
		String sql = "SELECT id,uid,taskid,count,taskstarttime,receive FROM proxytask";
		CachedRowSet resultSet = SqlHelper.getResultSet(sql);
		try {
			while (resultSet.next()) {
				ProxyTask pt = new ProxyTask();
				pt.id = resultSet.getInt("id");
				pt.uid = resultSet.getInt("uid");
				pt.taskid = resultSet.getInt("taskid");
				pt.count = resultSet.getLong("count");
				pt.taskstarttime = resultSet.getLong("taskstarttime");
				pt.receive = resultSet.getInt("receive");
				listTask.add(pt);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listTask;
	}
	public static ArrayList<ProxyTask> selectUserProxy(int uid) {
		ArrayList<ProxyTask> listTask = new ArrayList<>();
		String sql = "SELECT id,uid,taskid,count,taskstarttime,receive FROM proxytask WHERE uid = ?";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		CachedRowSet resultSet = SqlHelper.getResultSet(sql, p1);
		try {
			while (resultSet.next()) {
				ProxyTask pt = new ProxyTask();
				pt.id = resultSet.getInt("id");
				pt.uid = resultSet.getInt("uid");
				pt.taskid = resultSet.getInt("taskid");
				pt.count = resultSet.getLong("count");
				pt.taskstarttime = resultSet.getLong("taskstarttime");
				pt.receive = resultSet.getInt("receive");
				listTask.add(pt);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listTask;
	}
}
